package com.threadExample;

import java.util.concurrent.Semaphore;

/**
 * Created by yijy on 2018/4/12.
 */
public class SemaphoreTest {

    public static void main(String[] args) {
        int nums=8;
        Semaphore semaphore = new Semaphore(5);
        for(int i=0;i<nums;i++){
            new SemaphoreTest().new Work(semaphore,i).start();
        }
    }

    class Work extends Thread{

        private Semaphore semaphore;

        private int count;

        public Work(Semaphore semaphore,int count){
            this.semaphore = semaphore;
            this.count = count;
        }

        public void run(){
            try {
                semaphore.acquire();
                System.out.println("工人"+count+":正在占用机器生产。。。");
                Thread.sleep(2000);
                semaphore.release();
                System.out.println("工人"+count+":生产完毕！");
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }
}

